using Microsoft.EntityFrameworkCore;
using System;
using Terra.Data.Entities.Identity;

namespace Terra.Data.SeedData.IdentitySeedData;

/// <summary>
/// 权限种子数据提供者
/// </summary>
public class PermissionSeedData : ISeedDataProvider
{
    private readonly DateTime _now = DateTime.UtcNow;
    private readonly Guid _adminId = UserSeedData.SystemUserIds.AdminUserId;

    /// <summary>
    /// 创建权限
    /// </summary>
    private Permission CreatePermission(
        Guid id,
        string name,
        string code,
        string type,
        Guid menuId,
        string description = null,
        bool isSystem = true)
    {
        return new Permission
        {
            Id = id,
            Name = name,
            Code = code,
            Type = type,
            MenuId = menuId,
            Description = description,
            Status = 1,
            IsSystem = isSystem,
            CreatedTime = _now,
            CreatedBy = _adminId,
            LastModifiedTime = _now,
            LastModifiedBy = _adminId,
            IsDeleted = false
        };
    }

    /// <summary>
    /// 配置权限种子数据
    /// </summary>
    public void Configure(ModelBuilder modelBuilder)
    {
        #region 仪表盘权限

        // 仪表盘目录权限
        modelBuilder.Entity<Permission>().HasData(CreatePermission(
            Guid.NewGuid(), "仪表盘目录", "dashboard", "menu",
            MenuSeedData.SystemMenuIds.DashboardId, "仪表盘目录访问权限"));

        // 数据分析权限
        modelBuilder.Entity<Permission>().HasData(CreatePermission(
            Guid.NewGuid(), "数据分析", "dashboard:analytics", "menu",
            MenuSeedData.SystemMenuIds.AnalyticsId, "数据分析页面访问权限"));

        // 工作台权限
        modelBuilder.Entity<Permission>().HasData(CreatePermission(
            Guid.NewGuid(), "工作台", "dashboard:workspace", "menu",
            MenuSeedData.SystemMenuIds.WorkspaceId, "工作台页面访问权限"));

        #endregion

        #region 系统管理权限

        // 系统管理目录权限
        modelBuilder.Entity<Permission>().HasData(CreatePermission(
            Guid.NewGuid(), "系统管理", "system", "menu",
            MenuSeedData.SystemMenuIds.SystemId, "系统管理目录访问权限"));

        // 菜单管理权限
        modelBuilder.Entity<Permission>().HasData(
            CreatePermission(Guid.NewGuid(), "菜单管理", "system:menu", "menu",
                MenuSeedData.SystemMenuIds.MenuManagementId, "菜单管理页面访问权限"),
            CreatePermission(Guid.NewGuid(), "菜单创建", "system:menu:create", "operation",
                MenuSeedData.SystemMenuIds.ButtonIds.MenuCreateId, "创建菜单的权限"),
            CreatePermission(Guid.NewGuid(), "菜单修改", "system:menu:update", "operation",
                MenuSeedData.SystemMenuIds.ButtonIds.MenuUpdateId, "修改菜单的权限"),
            CreatePermission(Guid.NewGuid(), "菜单删除", "system:menu:delete", "operation",
                MenuSeedData.SystemMenuIds.ButtonIds.MenuDeleteId, "删除菜单的权限")
        );

        // 用户管理权限
        modelBuilder.Entity<Permission>().HasData(
            CreatePermission(Guid.NewGuid(), "用户管理", "system:user", "menu",
                MenuSeedData.SystemMenuIds.UserManagementId, "用户管理页面访问权限"),
            CreatePermission(Guid.NewGuid(), "用户创建", "system:user:create", "operation",
                MenuSeedData.SystemMenuIds.ButtonIds.UserCreateId, "创建用户的权限"),
            CreatePermission(Guid.NewGuid(), "用户修改", "system:user:update", "operation",
                MenuSeedData.SystemMenuIds.ButtonIds.UserUpdateId, "修改用户的权限"),
            CreatePermission(Guid.NewGuid(), "用户删除", "system:user:delete", "operation",
                MenuSeedData.SystemMenuIds.ButtonIds.UserDeleteId, "删除用户的权限")
        );

        // 角色管理权限
        modelBuilder.Entity<Permission>().HasData(
            CreatePermission(Guid.NewGuid(), "角色管理", "system:role", "menu",
                MenuSeedData.SystemMenuIds.RoleManagementId, "角色管理页面访问权限"),
            CreatePermission(Guid.NewGuid(), "角色创建", "system:role:create", "operation",
                MenuSeedData.SystemMenuIds.ButtonIds.RoleCreateId, "创建角色的权限"),
            CreatePermission(Guid.NewGuid(), "角色修改", "system:role:update", "operation",
                MenuSeedData.SystemMenuIds.ButtonIds.RoleUpdateId, "修改角色的权限"),
            CreatePermission(Guid.NewGuid(), "角色删除", "system:role:delete", "operation",
                MenuSeedData.SystemMenuIds.ButtonIds.RoleDeleteId, "删除角色的权限")
        );

        // 部门管理权限
        modelBuilder.Entity<Permission>().HasData(
            CreatePermission(Guid.NewGuid(), "部门管理", "system:dept", "menu",
                MenuSeedData.SystemMenuIds.DeptManagementId, "部门管理页面访问权限"),
            CreatePermission(Guid.NewGuid(), "部门创建", "system:dept:create", "operation",
                MenuSeedData.SystemMenuIds.ButtonIds.DeptCreateId, "创建部门的权限"),
            CreatePermission(Guid.NewGuid(), "部门修改", "system:dept:update", "operation",
                MenuSeedData.SystemMenuIds.ButtonIds.DeptUpdateId, "修改部门的权限"),
            CreatePermission(Guid.NewGuid(), "部门删除", "system:dept:delete", "operation",
                MenuSeedData.SystemMenuIds.ButtonIds.DeptDeleteId, "删除部门的权限")
        );

        #endregion

        #region 日志管理权限

        // 日志管理目录权限
        modelBuilder.Entity<Permission>().HasData(CreatePermission(
            Guid.NewGuid(), "日志管理", "log", "menu",
            MenuSeedData.SystemMenuIds.LogId, "日志管理目录访问权限"));

        // 登录日志权限
        modelBuilder.Entity<Permission>().HasData(
            CreatePermission(Guid.NewGuid(), "登录日志", "log:login", "menu",
                MenuSeedData.SystemMenuIds.LoginLogId, "登录日志页面访问权限"),
            CreatePermission(Guid.NewGuid(), "登录日志清理", "log:login:clean", "operation",
                MenuSeedData.SystemMenuIds.ButtonIds.LoginLogCleanId, "清理登录日志的权限")
        );

        // 操作日志权限
        modelBuilder.Entity<Permission>().HasData(
            CreatePermission(Guid.NewGuid(), "操作日志", "log:operation", "menu",
                MenuSeedData.SystemMenuIds.OperationLogId, "操作日志页面访问权限"),
            CreatePermission(Guid.NewGuid(), "操作日志清理", "log:operation:clean", "operation",
                MenuSeedData.SystemMenuIds.ButtonIds.OperationLogCleanId, "清理操作日志的权限")
        );

        #endregion
    }

    /// <summary>
    /// 获取提供者优先级（在菜单种子数据之后）
    /// </summary>
    public int Order => 40;
}